import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select


def sign_in(uid, pwd):
    # set to no-window

    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")

    # simulate a browser to open the website
    # browser = webdriver.Chrome()
    browser = webdriver.Chrome('D:\Anaconda3\envs\pytest\Scripts\chromedriver.exe',options=chrome_options)
    browser.get("http://login.cuit.edu.cn/Login/xLogin/Login.asp")

    try:
        # input uid and password
        browser.find_element_by_id("txtId").send_keys(uid)
        browser.find_element_by_id("txtMM").send_keys(pwd)
        # click to sign in
        browser.find_element_by_id("IbtnEnter").click()
        sleep(1)

        # click to link
        str=time.strftime("%m%d", time.localtime())+'疫情防控——师生健康状态采集'
        browser.find_element_by_link_text(str).click()

        # click to fill in
        sel_location=browser.find_element_by_name("sF21650_1")
        sel_location.click()
        sleep(1)
        Select(sel_location).select_by_value('1')

        sel_location_state=browser.find_element_by_name("sF21650_5")
        sel_location_state.click()
        sleep(1)
        Select(sel_location_state).select_by_value('1')

        sel_work_state = browser.find_element_by_name("sF21650_6")
        sel_work_state.click()
        sleep(1)
        Select(sel_work_state).select_by_value('1')

        sel_helth_state = browser.find_element_by_name("sF21650_7")
        sel_helth_state.click()
        sleep(1)
        Select(sel_helth_state).select_by_value('1')

        sel_life_state = browser.find_element_by_name("sF21650_8")
        sel_life_state.click()
        sleep(1)
        Select(sel_life_state).select_by_value('1')

        sel_family_state = browser.find_element_by_name("sF21650_9")
        sel_family_state.click()
        sleep(1)
        Select(sel_family_state).select_by_value('1')

        browser.find_element_by_name("sF21912_1").clear()
        browser.find_element_by_name("sF21912_1").send_keys('北街')
        browser.find_element_by_name("sF21912_2").clear()
        browser.find_element_by_name("sF21912_2").send_keys('吃饭')

        sel_start_data = browser.find_element_by_name("sF21912_3")
        sel_start_data.click()
        sleep(1)
        Select(sel_start_data).select_by_value('1')

        sel_start_time = browser.find_element_by_name("sF21912_4")
        sel_start_time.click()
        sleep(1)
        Select(sel_start_time).select_by_value('06')

        sel_end_data = browser.find_element_by_name("sF21912_5")
        sel_end_data.click()
        sleep(1)
        Select(sel_end_data).select_by_value('1')

        sel_end_time = browser.find_element_by_name("sF21912_6")
        sel_end_time.click()
        sleep(1)
        Select(sel_end_time).select_by_value('23')

        # Submitted to clock in
        browser.find_element_by_name("B2").click()
        sleep(1)
        browser.switch_to.alert.accept()

    except Exception as e:
        msg = "while signing in for user " + uid + " there is an exception: \n" + str(e)
        print(msg)
    finally:
        browser.close()

if __name__ == '__main__':
    sign_in(id, password)




